package favorite

import (
	"context"
	"net/http"

	"github.com/gin-gonic/gin"

	"mxshop-api/shared/auth"
	"mxshop-api/shared/utils"
	"mxshop-api/userop-web/api"
	"mxshop-api/userop-web/forms"
	"mxshop-api/userop-web/global"
	goodspb "mxshop-api/userop-web/proto/goods/gen/v1"
	useroppb "mxshop-api/userop-web/proto/userop/gen/v1"
)

type APIGoodsInfo struct {
	GoodsID         int32  `json:"goods_id,omitempty"`
	Name            string `json:"name,omitempty"`
	ShopPrice       string `json:"shop_price,omitempty"`
	GoodsFrontImage string `json:"goods_front_image,omitempty"`
}

func GoodsInfoPbToAPI(g *goodspb.GoodsInfoResponse) APIGoodsInfo {
	return APIGoodsInfo{
		GoodsID:         g.Id,
		Name:            g.Name,
		ShopPrice:       utils.PriceInt(g.ShopPrice).String(),
		GoodsFrontImage: g.GoodsFrontImage,
	}
}

func New(c *gin.Context) {
	// 1.获取用户ID
	userID, err := auth.UserIDFromContext(c)
	if err != nil {
		return
	}

	// 2.校验参数
	var favorite forms.FavoriteForm
	err = c.ShouldBind(&favorite)
	if err != nil {
		api.HandleValidatorError(c, err)
		return
	}

	// 3.调用goods-service去判断商品是否存在
	goodsCli := goodspb.NewGoodsClient(global.GoodsConn)
	_, err = goodsCli.GetGoodsDetail(context.Background(), &goodspb.GoodInfoRequest{Id: favorite.GoodsID})
	if err != nil {
		api.HandleGrpcErrorToHttp(c, err)
		return
	}

	// 4.调用userop-service去新增收藏
	useropCli := useroppb.NewUserFavServiceClient(global.UserOpConn)
	_, err = useropCli.AddUserFav(context.Background(), &useroppb.UserFav{
		UserId:  int32(userID),
		GoodsId: favorite.GoodsID,
	})
	if err != nil {
		api.HandleGrpcErrorToHttp(c, err)
		return
	}

	c.Status(http.StatusCreated)
}

func List(c *gin.Context) {
	userID, err := auth.UserIDFromContext(c)
	if err != nil {
		return
	}

	// 查询用户所有的收藏
	useropCli := useroppb.NewUserFavServiceClient(global.UserOpConn)
	favListResp, err := useropCli.GetFavList(context.Background(), &useroppb.GetFavListRequest{UserId: int32(userID)})
	if err != nil {
		api.HandleGrpcErrorToHttp(c, err)
		return
	}

	if len(favListResp.UserFaves) == 0 {
		c.JSON(http.StatusOK, gin.H{"total": 0})
		return
	}

	goodsIDs := make([]int32, 0, len(favListResp.UserFaves))
	for _, fav := range favListResp.UserFaves {
		goodsIDs = append(goodsIDs, fav.GoodsId)
	}

	// 查询用户收藏商品对应的信息
	goodsCli := goodspb.NewGoodsClient(global.GoodsConn)
	goodsResp, err := goodsCli.BatchGetGoods(context.Background(), &goodspb.BatchGoodsIdInfo{Id: goodsIDs})
	if err != nil {
		api.HandleGrpcErrorToHttp(c, err)
		return
	}

	goodsInfo := make([]APIGoodsInfo, 0, len(goodsResp.Data))
	for _, goods := range goodsResp.Data {
		goodsInfo = append(goodsInfo, GoodsInfoPbToAPI(goods))
	}

	c.JSON(http.StatusOK, gin.H{
		"total": favListResp.Total,
		"data":  goodsInfo,
	})
}

func Detail(c *gin.Context) {
	userID, err := auth.UserIDFromContext(c)
	if err != nil {
		return
	}

	var favorite forms.FavoriteForm
	err = c.ShouldBindUri(&favorite)
	if err != nil {
		api.HandleValidatorError(c, err)
		return
	}

	useropCli := useroppb.NewUserFavServiceClient(global.UserOpConn)
	_, err = useropCli.GetUserFavDetail(context.Background(), &useroppb.UserFav{
		UserId:  int32(userID),
		GoodsId: favorite.GoodsID,
	})
	if err != nil {
		api.HandleGrpcErrorToHttp(c, err)
		return
	}

	c.Status(http.StatusOK)
}

func Delete(c *gin.Context) {
	userID, err := auth.UserIDFromContext(c)
	if err != nil {
		return
	}

	var favorite forms.FavoriteForm
	err = c.ShouldBindUri(&favorite)
	if err != nil {
		api.HandleValidatorError(c, err)
		return
	}

	useropCli := useroppb.NewUserFavServiceClient(global.UserOpConn)
	_, err = useropCli.DeleteUserFav(context.Background(), &useroppb.UserFav{
		UserId:  int32(userID),
		GoodsId: favorite.GoodsID,
	})
	if err != nil {
		api.HandleGrpcErrorToHttp(c, err)
		return
	}

	c.Status(http.StatusNoContent)
}
